import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("日期选择器Pro")
    color: "#404040"

    property int currentYear: new Date().getFullYear()
    property int currentMonth: new Date().getMonth() + 1
    property int currentDay: new Date().getDate()

    // 获取指定年月的天数
    function getDaysInMonth(year, month) {
        return new Date(year, month, 0).getDate()
    }

    Rectangle {
        anchors.centerIn: parent
        width: row.width + 40
        height: row.height + 40
        color: "#202020"
        radius: 10

        RowLayout {
            id: row
            anchors.centerIn: parent
            spacing: 20

            // 年份选择器
            Tumbler {
                id: yearTumbler
                Layout.preferredWidth: 120
                Layout.preferredHeight: 300
                model: ListModel {
                    id: yearModel
                }
                visibleItemCount: 5
                delegate: Text {
                    text: modelData
                    color: "white"
                    font.pixelSize: 16 + (1 - Math.abs(Tumbler.displacement)) * 8
                    opacity: 0.5 + (1 - Math.abs(Tumbler.displacement)) * 0.5
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    width: 120
                }
            }

            // 月份选择器
            Tumbler {
                id: monthTumbler
                Layout.preferredWidth: 80
                Layout.preferredHeight: 300
                model: 12
                visibleItemCount: 5
                delegate: Text {
                    text: (modelData + 1) + "月"
                    color: "white"
                    font.pixelSize: 16 + (1 - Math.abs(Tumbler.displacement)) * 8
                    opacity: 0.5 + (1 - Math.abs(Tumbler.displacement)) * 0.5
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    width: 80
                }
            }

            // 日期选择器
            Tumbler {
                id: dayTumbler
                Layout.preferredWidth: 80
                Layout.preferredHeight: 300
                model: ListModel {
                    id: dayModel
                }
                visibleItemCount: 5
                delegate: Text {
                    text: modelData + "日"
                    color: "white"
                    font.pixelSize: 16 + (1 - Math.abs(Tumbler.displacement)) * 8
                    opacity: 0.5 + (1 - Math.abs(Tumbler.displacement)) * 0.5
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    width: 80
                }
            }
        }
    }

    // 更新年份模型
    function updateYearModel() {
        var years = []
        for (var i = currentYear - 50; i <= currentYear + 50; ++i) {
            years.push(i)
        }
        yearTumbler.model = years
        yearTumbler.currentIndex = 50  // 设置当前年份位置
    }

    // 更新日期模型
    function updateDayModel() {
        var year = yearTumbler.model[yearTumbler.currentIndex]
        var month = monthTumbler.currentIndex + 1
        var days = getDaysInMonth(year, month)
        
        var daysArray = []
        for (var i = 1; i <= days; ++i) {
            daysArray.push(i)
        }
        dayTumbler.model = daysArray
        
        // 确保当前选中的日期有效
        if (dayTumbler.currentIndex >= days) {
            dayTumbler.currentIndex = days - 1
        }
    }

    // 监听年月变化，更新日期
    Connections {
        target: yearTumbler
        function onCurrentIndexChanged() { updateDayModel() }
    }
    
    Connections {
        target: monthTumbler
        function onCurrentIndexChanged() { updateDayModel() }
    }

    Component.onCompleted: {
        updateYearModel()
        monthTumbler.currentIndex = currentMonth - 1
        updateDayModel()
        dayTumbler.currentIndex = currentDay - 1
    }
}
